package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsMarketingAdsMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMarketingAdsMapperImpl;
import cc.rengu.igas.bsps.common.entity.MarketingAds;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.MarketAdInfoBean;
import cc.rengu.igas.bsps.facade.request.MarketAdvertQueryRequest;
import cc.rengu.igas.bsps.facade.response.MarketAdvertQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 轮播营销广告查询.
 */
public class MarketAdvertQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MarketAdvertQueryRequest marketAdvertQueryRequest = new MarketAdvertQueryRequest();
        ConvertUtil.convertOutput(marketAdvertQueryRequest);
        return marketAdvertQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MarketAdvertQueryRequest marketAdvertQueryRequest = (MarketAdvertQueryRequest) request;

        String instId = marketAdvertQueryRequest.getHeader().getInstId();

        /* 根据机构号查询内管平台 营销广告管理表 */
        BspsMarketingAdsMapper bspsMarketingAdsMapper = new BspsMarketingAdsMapperImpl();
        //adType  00：商户服务轮播广告   01：交易成功推送广告
        List<MarketingAds> marketingAdsList = bspsMarketingAdsMapper.selectBspsMarketingAdsListByInstIdAndAdType(instId, "00");
        List<MarketAdInfoBean> marketAdInfoBeanList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(marketingAdsList)) {
            marketAdInfoBeanList = marketingAdsList.stream().map(this::convertMarketingAds).collect(Collectors.toList());
        }

        /* 拼装应答结果 */
        MarketAdvertQueryResponse marketAdvertQueryResponse = new MarketAdvertQueryResponse();
        marketAdvertQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        marketAdvertQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        marketAdvertQueryResponse.setMarketAdInfoBeanList(marketAdInfoBeanList);
        BizResponse<MarketAdvertQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(marketAdvertQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MarketAdvertQueryResponse marketAdvertQueryResponse = (MarketAdvertQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(marketAdvertQueryResponse);
    }

    /**
     * 转换为facade层的bean
     *
     * @param marketingAds 营销广告
     * @return 广告
     */
    private MarketAdInfoBean convertMarketingAds(MarketingAds marketingAds) {
        MarketAdInfoBean marketAdInfoBean = new MarketAdInfoBean();
        if (!StringUtil.isEmptyOrNull(marketingAds.getImageLocalPath())) {
            marketAdInfoBean.setMarketImagePath(Base64.encodeBase64String(marketingAds.getImageLocalPath().getBytes()));
        }
        if (!StringUtil.isEmptyOrNull(marketingAds.getMarketingPagePath())) {
            marketAdInfoBean.setMarketUrlPath(Base64.encodeBase64String(marketingAds.getMarketingPagePath().getBytes()));
        }

        return marketAdInfoBean;
    }
}
